﻿Imports VN.INS.BL.BC
Imports System.Data

Partial Class Devida_DigitaCodigo
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (Not (Page.IsPostBack)) Then
            'capturar parametros
            ViewState("POA") = Request.QueryString("POA")
            ViewState("Version") = Request.QueryString("Version")
            CargarComponente("POA-" & ViewState("POA").ToString, ViewState("Version").ToString)
            CargaFisico("POA-" & ViewState("POA").ToString, ViewState("Version").ToString)
        End If
    End Sub

    'Cargar grilla componente
    Private Sub CargarComponente(ByVal strPOA As String, ByVal strVersion As String)
        Dim objDt As DataTable
        Dim objMantPOA As MantenimientoPoaBC
        Try
            objDt = New DataTable
            objMantPOA = New MantenimientoPoaBC
            objDt = objMantPOA.CargaComponenteEdicion(strPOA, strVersion)
            gvComponente.DataSource = objDt
            gvComponente.DataBind()
        Catch ex As Exception
            alerta.Mostrar_Alerta("Error", ex.Message.ToString)
        End Try
    End Sub

    'Cargar grilla de metas fisicos
    Private Sub CargaFisico(ByVal strPOA As String, ByVal strVersion As String)
        Dim objDt As DataTable
        Dim objMantPOA As MantenimientoPoaBC
        Try
            objDt = New DataTable
            objMantPOA = New MantenimientoPoaBC
            objDt = objMantPOA.CargaFisicoEdicion(strPOA, strVersion)
            gvFisico.DataSource = objDt
            gvFisico.DataBind()
        Catch ex As Exception
            alerta.Mostrar_Alerta("Error", ex.Message.ToString)
        End Try
    End Sub

    'salir
    Protected Sub ibn_salir_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ibn_salir.Click
        Response.Redirect("MantenimientoPoa.aspx")
    End Sub

    Protected Sub btn_guardar_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btn_guardar.Click
        For i As Int16 = 0 To gvComponente.Rows.Count - 1   'recorremos la grilla componente
            Dim hfCodigo As HiddenField = CType(gvComponente.Rows(i).FindControl("hfCodigo"), HiddenField)
            Dim txtCodigo As TextBox = CType(gvComponente.Rows(i).FindControl("txtCodigo"), TextBox)
            'editamos los codigos
            EditarComponente(txtCodigo.Text, hfCodigo.Value, "POA-" & ViewState("POA").ToString, ViewState("Version").ToString)
        Next
        alerta.Mostrar_Alerta("Aviso", "Datos guardados correctamente")
        'cargamos las grillas
        CargarComponente("POA-" & ViewState("POA").ToString, ViewState("Version").ToString)
        CargaFisico("POA-" & ViewState("POA").ToString, ViewState("Version").ToString)
    End Sub

    'editar componente
    Private Sub EditarComponente(ByVal strCodigo As String, ByVal strComponente As String, ByVal strPOA As String, ByVal strVersion As String)
        Dim objMantenimiento As MantenimientoPoaBC
        Try
            objMantenimiento = New MantenimientoPoaBC
            objMantenimiento.EditarCodigoComponente(strCodigo, strComponente, strPOA, strVersion)
        Catch ex As Exception
            alerta.Mostrar_Alerta("Error", ex.Message.ToString)
        End Try
    End Sub

    'Editar metas fisicas
    Private Sub EditarFisico(ByVal strComponente As String, ByVal strPOA As String, ByVal strIPAF As String, ByVal strVersion As String, ByVal strCodigo As String)
        Dim objMantenimiento As MantenimientoPoaBC
        Try
            objMantenimiento = New MantenimientoPoaBC
            objMantenimiento.EditarCodigoFisico(strComponente, strPOA, strIPAF, strVersion, strCodigo)
        Catch ex As Exception
            alerta.Mostrar_Alerta("Error", ex.Message.ToString)
        End Try
    End Sub

    Protected Sub btnGuardarFisico_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnGuardarFisico.Click
        For i As Int16 = 0 To gvFisico.Rows.Count - 1   'recorremos grilla fisico
            Dim hfCodigo As HiddenField = CType(gvFisico.Rows(i).FindControl("hfCodigo"), HiddenField)
            Dim hfComponente As HiddenField = CType(gvFisico.Rows(i).FindControl("hfComponente"), HiddenField)
            Dim txtCodigo As TextBox = CType(gvFisico.Rows(i).FindControl("txtCodigo"), TextBox)
            'Editamos los codigos de metas fisicas
            EditarFisico(hfComponente.Value, "POA-" & ViewState("POA").ToString, hfCodigo.Value, ViewState("Version").ToString, txtCodigo.Text)
        Next
        alerta.Mostrar_Alerta("Aviso", "Datos guardados correctamente")
        'cargar grillas
        CargarComponente("POA-" & ViewState("POA").ToString, ViewState("Version").ToString)
        CargaFisico("POA-" & ViewState("POA").ToString, ViewState("Version").ToString)
    End Sub
End Class
